﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

#endregion

#region COPYRIGHT

/*
    Copyright (c) 2008, 2009, 2010 
        Roland Rosenkranz (Glatzemann@email.de)
*/

#endregion

#region LICENSE

/*
    This file is part of starLiGHT.Collision.

    starLiGHT.Collision is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    starLiGHT.Collision is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with starLiGHT.Collision.  If not, see <http://www.gnu.org/licenses/>.

    
    ADDITIONAL (commercial) LICENSES for starLiGHT.Collision are available on request.
*/

#endregion

namespace starLiGHT.Physic.TwoD
{
    public class SKViscosity : SmoothingKernel
    {
        public SKViscosity(float kernelSize)
            : base(kernelSize)
        { }

        internal override void calculateFactor()
        {
            m_factor = (float)(15.0f / (2.0f * Math.PI * m_kernelSize3));
        }

        internal override float calculate(Vector2 distance)
        {
            float lenSq = distance.LengthSquared();
            if (lenSq > base.m_kernelSizeSq)
            {
                return 0.0f;
            }

            if (lenSq < PhysicSettings.FLT_EPSILON)
            {
                lenSq = PhysicSettings.FLT_EPSILON;
            }

            float len = (float)System.Math.Sqrt(lenSq);
            float len3 = len * len * len;
            return m_factor * (((-len3 / (2.0f * m_kernelSize3)) + (lenSq / m_kernelSizeSq) + (m_kernelSize / (2.0f * len))) - 1.0f);
        }

        internal override Vector2 calculateGradient(Vector2 distance)
        {
            throw new NotImplementedException();
        }

        internal override float calculateLaplacian(Vector2 distance)
        {
            float lenSq = distance.LengthSquared();
            if (lenSq > m_kernelSizeSq) return 0.0f;
            if (lenSq < PhysicSettings.FLT_EPSILON) return PhysicSettings.FLT_EPSILON;

            float len = (float)System.Math.Sqrt(lenSq);
            return m_factor * (6.0f / m_kernelSize3) * (m_kernelSize - len);
        }
    }
}
